<?php 

if( !defined('IN_INDEX') )
{
	printError("ACCESS DENIED", "This page is hidden.");
	exit;
}
include './inc/functions_blog.php';
include './inc/functions_like.php';

$training_id = intval($_POST['tid']);

$comments = getBlogCommentsSort(" WHERE post_type='training' AND post_id='" . $training_id . "' ",$USER->getSortComments());

$response = "";

$dateformat = $CONFIG['date_format'];
$dateformat_long = $CONFIG['date_format_long'];

/**
$COMMENT['id']
$COMMENT['type'] -eksempel: training, news etc. 
$COMMENT['post_id'] - id til treningen, trengs heller ikke i dette tilfelle
$COMMENT['user_id'] - id til brukeren som poster. 
$COMMENT['date'] - self explained
$COMMENT['comment'] - selve kommentaren
$COMMENT['rating'] - placeholder til evt. ? kunne rangere en post.
**/
$gavatar = "";
foreach($comments as $k=>$v){
	$COMMENT = $v;
	$U_PROFILE = new User('id', $COMMENT['user_id']);
	$comment_id = $COMMENT['id'];
	$comment_head = translate('comment_from') . " " . $U_PROFILE->getFirstname() . " " . formatDate($COMMENT['date'], $CONFIG['date_format_long']) . "\n";

	$comment_text = ($COMMENT['comment']);
	
	if($U_PROFILE->getAvatarpath() != "")
	{
		$profile_pic_path = $CONFIG['user_data_path'] . $U_PROFILE->getId() . "/thumbs_" . $U_PROFILE->getAvatarpath();
		if(file_exists($profile_pic_path))
		{
			$gavatar= "<a href='index.php?action=profile&amp;id=" . $U_PROFILE->getId() . "'>
			<img style='background: url(" . $profile_pic_path . ") no-repeat 50%; height: 30px; width: 30px;' src=\"" . $profile_pic_path . "\" alt=\"" . translate('profile_picture') . "\" /></a>\n";
		}
	}
	else
	{
		if($U_PROFILE->getGender() == "female")
		{
			$gavatar= "<a href='index.php?action=profile&amp;id=" . $U_PROFILE->getId() . "'>
			<img src=\"" . $CONFIG['user_data_path'] . "profile_female.gif\" height='30' width='30' alt=\"" . translate('profile_picture') . "\" /></a>\n";
		}
		else
		{
			$gavatar= "<a href='index.php?action=profile&amp;id=" . $U_PROFILE->getId() . "'>
			<img src=\"" . $CONFIG['user_data_path'] . "profile_male.gif\" height='30' width='30' alt=\"" . translate('profile_picture') . "\" /></a>\n";
		}
	}
	
	$COMMENT['comment_id'] = $comment_id;
	$like_comment = "<a href='javascript:void(0);' onclick=\"return open_who_like_comment(". $training_id .",". $COMMENT['comment_id'] .");\"><img src='./img/thumb_up.png' alt='". translate('like') ."' /></a>
						<span class='like_comment' rel='". $COMMENT['comment_id'] ."'>". count_numberOfPersons_like_comment($COMMENT['user_id'], $training_id, $COMMENT['comment_id']);
	if(is_like_comment($COMMENT['user_id'],$_SESSION['user_id'], $training_id, $COMMENT['comment_id'])){
		$like_comment .= " <a href='javascript:void(0);' onclick=\"like_unlike_comment('unlike_comment',".$_SESSION['user_id'].",". $COMMENT['user_id'].",". $training_id.",".$COMMENT['comment_id'] .");\" >". translate('unlike')."</a>";
	}else{
		$like_comment .= " <a href='javascript:void(0);' onclick=\"like_unlike_comment('like_comment',".$_SESSION['user_id'].",". $COMMENT['user_id'].",". $training_id.",".$COMMENT['comment_id'] .");\" >". translate('like')."</a>";
	}
	$like_comment .= "</span>";
	$like_comment .= "&nbsp;".ui_aj_follow_this_user($COMMENT['user_id'], $COMMENT['user_id']);
	
	$response .= "<li class='comment'>";
	$response .= "<div class='info'>";
	$response .= "<div class='date2'>";
	$response .= "<div style='float: left; clear: right; padding-right: 3px;'>".$gavatar."</div>";;
	$response .= "<a href='index.php?action=profile&amp;id=".$COMMENT['user_id']."' >"; 
	$response .= "".htmlentities($U_PROFILE->getFirstname())." ". htmlentities($U_PROFILE->getLastname())." (" . ucf(getCountryName(htmlentities($U_PROFILE->getCountry()))) . ") " .
				  "</a>";
	//$response .= $comment_head;
	$response .= " ".translate('the')." ";			 
    $response .= formatDate($COMMENT['date'], $dateformat_long) . "";        
    if($COMMENT['date_update'] != 0)
	{
		 $response .=  " (" . ucf(translate('updated')) . " " . translate('for2') . " " . getHowLongAgo($COMMENT['date_update']). ")"; 
	}
	$response .= "<br/>".$like_comment;
	$response .= "</div>" ;          
  	$response .= "<div class='fixed'></div>";
	$response .= "<div id='comment_$comment_id' class='content'>";
	$response .= nl2br(formatComment(htmlentities($COMMENT['comment']))); 
	$response .= "</div></div><div class='fixed'></div></li>";			
}
if($USER->getAvatarpath() != "")
	{
		$profile_pic_path = $CONFIG['user_data_path'] . $USER->getId() . "/thumbs_" . $USER->getAvatarpath();
		if(file_exists($profile_pic_path))
		{
			$gavatar= "<a href='index.php?action=profile&amp;id=" . $USER->getId() . "'>
			<img style='background: url(" . $profile_pic_path . ") no-repeat 50%; height: 30px; width: 30px;' src=\"" . $profile_pic_path . "\" alt=\"" . translate('profile_picture') . "\" /></a>\n";
		}
	}
	else
	{
		if($USER->getGender() == "female")
		{
			$gavatar= "<a href='index.php?action=profile&amp;id=" . $USER->getId() . "'>
			<img src=\"" . $CONFIG['user_data_path'] . "profile_female.gif\" height='30' width='30' alt=\"" . translate('profile_picture') . "\" /></a>\n";
		}
		else
		{
			$gavatar= "<a href='index.php?action=profile&amp;id=" . $USER->getId() . "'>
			<img src=\"" . $CONFIG['user_data_path'] . "profile_male.gif\" height='30' width='30' alt=\"" . translate('profile_picture') . "\" /></a>\n";
		}
	}

$response .= "<li class='comment'>";
$response .= "<div class='info'>";
$response .= $gavatar;
$response .= "<input type='text' id='comment_new' name='comment_new' size='50' maxlength='250' 
				onkeypress='comment(this,".$_POST['owner_id'].",$training_id,event);' />";
$response .= "</div><div class='fixed'></div>";
$response .= "<div class='content'>&nbsp;</div>";
$response .= "<div class='fixed'></div></li>";		
echo $response;

exit();